//% color="#29be3a" iconWidth=50 iconHeight=40
namespace expansion{
        
    //% block="read [A] [B]" blockType="reporter"
    //% A.shadow="dropdown" A.options="A" 
    //% B.shadow="dropdown" B.options="B" 
    export function myBlock1(parameter: any, block: any) {
        let A= parameter.A.code
        let B= parameter.B.code
        let B1
        switch(B) {
            case "Analog": B1 = "ADC"; break;
            case "Digital_IN": B1 = "GPIO_IN"; break;
            case "DHT11Temperature": B1 = "DHT11"; break;
            case "DHT11Humidity": B1 = "DHT11"; break;
            case "DHT22Temperature": B1 = "DHT22"; break;
            case "DHT22Humidity": B1 = "DHT22"; break;
            case "DS18B20Temperature": B1 = "DS18B20"; break;
            case "i27SHT30Temperature": B1 = "ADC"; break;
            case "i27SHT30Humidity": B1 = "ADC"; break;
            case "i19WaterproofTemp": B1 = "ADC"; break;
        }
        Generator.addImport(`from pinpong.board import Board`)
        Generator.addImport(`from DFRobot_UnihikerExpansion import *`)
        Generator.addInit("Board_begin",`Board().begin()`)
        Generator.addInit("Expansion_begin",`eunihiker = UnihikerExpansion()`)
        Generator.addInit(`set_mode${A}`,`eunihiker.set_mode(IONum.${A}, IOType.${B1})`)

        Generator.addCode(`eunihiker.read_sensor(IONum.${A},SensorType.${B})`)
    }


    //% block="pin [A] output [H]" blockType="command"
    //% A.shadow="dropdown" A.options="A" 
    //% H.shadow="dropdown" H.options="H" 
    export function myBlock2(parameter: any, block: any) {
        let A= parameter.A.code
        let H= parameter.H.code
        Generator.addImport(`from pinpong.board import Board`)
        Generator.addImport(`from DFRobot_UnihikerExpansion import *`)
        Generator.addInit("Board_begin",`Board().begin()`)
        Generator.addInit("Expansion_begin",`eunihiker = UnihikerExpansion()`)
        Generator.addInit(`set_mode${A}`,`eunihiker.set_mode(IONum.${A}, IOType.GPIO_OUT)`)

        Generator.addCode(`eunihiker.set_gpio_state(IONum.${A},GpioState.${H})`)
    }


    //% block="pin [S] servo rotate [D] at [C] % speed" blockType="command"
    //% C.shadow="range"   C.params.min=0    C.params.max=100   C.defl=50
    //% D.shadow="dropdown" D.options="D" 
    //% S.shadow="dropdown" S.options="S" 
    export function myBlock3(parameter: any, block: any) {
        let C= parameter.C.code
        let D= parameter.D.code
        let S= parameter.S.code
        Generator.addImport(`from pinpong.board import Board`)
        Generator.addImport(`from DFRobot_UnihikerExpansion import *`)
        Generator.addInit("Board_begin",`Board().begin()`)
        Generator.addInit("Expansion_begin",`eunihiker = UnihikerExpansion()`)

        Generator.addCode(`eunihiker.set_servo360(ServoNum.${S},Direction.${D},${C})`)
    } 

    
    //% block="pin [S] servo stop " blockType="command"
    //% S.shadow="dropdown" S.options="S" 
    export function myBlock31(parameter: any, block: any) {
        let S= parameter.S.code
        Generator.addImport(`from pinpong.board import Board`)
        Generator.addImport(`from DFRobot_UnihikerExpansion import *`)
        Generator.addInit("Board_begin",`Board().begin()`)
        Generator.addInit("Expansion_begin",`eunihiker = UnihikerExpansion()`)

        Generator.addCode(`eunihiker.set_servo360(ServoNum.${S},Direction.Stop, 0)`)
    } 


    //% block="set pin [S] servo to [E] degree" blockType="command"
    //% S.shadow="dropdown" S.options="S" 
    //% E.shadow="angle"   E.params.edge=1
    export function myBlock4(parameter: any, block: any) {
        let S= parameter.S.code
        let E= parameter.E.code
        Generator.addImport(`from pinpong.board import Board`)
        Generator.addImport(`from DFRobot_UnihikerExpansion import *`)
        Generator.addInit("Board_begin",`Board().begin()`)
        Generator.addInit("Expansion_begin",`eunihiker = UnihikerExpansion()`)

        Generator.addCode(`eunihiker.set_servo_angle(ServoNum.${S}, ${E})`)
    }

    //% block="motor [M] rotate [D] at speed [G]" blockType="command"
    //% M.shadow="dropdown" M.options="M" 
    //% D.shadow="dropdown" D.options="D" 
    //% G.shadow="range"   G.params.min=0    G.params.max=255   G.defl=255
    export function myBlock5(parameter: any, block: any) {
        let M= parameter.M.code
        let D= parameter.D.code
        let G= parameter.G.code
        Generator.addImport(`from pinpong.board import Board`)
        Generator.addImport(`from DFRobot_UnihikerExpansion import *`)
        Generator.addInit("Board_begin",`Board().begin()`)
        Generator.addInit("Expansion_begin",`eunihiker = UnihikerExpansion()`)
        Generator.addInit("MPeriod1_2",`eunihiker.set_motor_period(MPeriod.MOTOR1_2, 255)`)
        Generator.addInit("MPeriod3_4",`eunihiker.set_motor_period(MPeriod.MOTOR3_4, 255)`)
             
        Generator.addCode(`eunihiker.set_motor(Motor.${M}, Direction.${D}, ${G})`)
    }

    //% block="motor stop [M]" blockType="command"
    //% M.shadow="dropdown" M.options="M" 
    export function myBlock6(parameter: any, block: any) {
        let M= parameter.M.code
        Generator.addImport(`from pinpong.board import Board`)
        Generator.addImport(`from DFRobot_UnihikerExpansion import *`)
        Generator.addInit("Board_begin",`Board().begin()`)
        Generator.addInit("Expansion_begin",`eunihiker = UnihikerExpansion()`)
        Generator.addInit("MPeriod1_2",`eunihiker.set_motor_period(MPeriod.MOTOR1_2, 255)`)
        Generator.addInit("MPeriod3_4",`eunihiker.set_motor_period(MPeriod.MOTOR3_4, 255)`)

        Generator.addCode(`eunihiker.stop_motor(Motor.${M});`)
    }

    //% block="read battery percentage" blockType="reporter"
    export function myBlock7(parameter: any, block: any) {
        Generator.addImport(`from pinpong.board import Board`)
        Generator.addImport(`from DFRobot_UnihikerExpansion import *`)
        Generator.addInit("Board_begin",`Board().begin()`)
        Generator.addInit("Expansion_begin",`eunihiker = UnihikerExpansion()`)

        Generator.addCode(`eunihiker.get_battery()`)
    }

    //% block="Initializes the RGB light" blockType="command"  
    export function myBlock8(parameter: any, block: any) { 

        Generator.addImport('from pinpong.board import Board,Pin');
        Generator.addImport('from pinpong.board import NeoPixel');
        Generator.addImport('from pinpong.extension.unihiker import *');
        Generator.addInit(`board_begin`,`Board().begin()`);
        Generator.addInit(`init_RGB`,`RGB = NeoPixel(Pin((Pin.P1)),4)`);
        // Generator.addCode(`RGB.brightness(128)`)
   
    }

    //% block="RGB lights Set [J]" blockType="command"  
    //% J.shadow="range" J.params.min=0 J.params.max=255 J.defl=128
    export function myBlock9(parameter: any, block: any) { 
        let J= parameter.J.code;

        Generator.addCode(`RGB.brightness(${J})`)
   
    }

    //% block="All RGB lights are off" blockType="command"  
    export function myBlock10(parameter: any, block: any) { 

        Generator.addCode(`RGB.clear()`)
   
    }

    //% block="Set the [COLOR] of the [NUMBER] light" blockType="command"  
    //% NUMBER.shadow="dropdown" NUMBER.options="NUMBER"
    //% COLOR.shadow="colorPalette"
    export function myBlock11_(parameter: any, block: any) { 
        let number=parameter.NUMBER.code;
        let color=parameter.COLOR.code; //获取传进来的值

        var r = 0;
        var g = 0;
        var b = 0;
        try {
            if ( color.length == 8 ) {//分别截取RGB值然后转换为数字值
                r = parseInt(color.substring(2, 4), 16);
                g = parseInt(color.substring(4, 6), 16);
                b = parseInt(color.substring(6, 8), 16);
            }
        } catch(e) {
            return '';
        }

        Generator.addCode(`RGB[${number}] = (${r},${g},${b})`)
   
    }

    //% block="RGB lights [NUMBER1] to [NUMBER2] display [COLOR]" blockType="command"  
    //% NUMBER1.shadow="dropdown" NUMBER1.options="NUMBER"
    //% NUMBER2.shadow="dropdown" NUMBER2.options="NUMBER"
    //% COLOR.shadow="colorPalette"
    export function myBlock11(parameter: any, block: any) { 
        let number1=parameter.NUMBER1.code;
        let number2=parameter.NUMBER2.code;
        let color=parameter.COLOR.code; 

        Generator.addCode(`RGB.range_color(${number1},${number2},${color})`)
   
    }

    //% block="RED[RED] GREEN[GREEN] BLUE[BLUE]" blockType="reporter"  
    //% RED.shadow="range" RED.params.min=0 RED.params.max=255 RED.defl=255
    //% GREEN.shadow="range" GREEN.params.min=0 GREEN.params.max=255 GREEN.defl=255
    //% BLUE.shadow="range" BLUE.params.min=0 BLUE.params.max=255 BLUE.defl=255
    export function myBlock12(parameter: any, block: any) { 
        let red=parameter.RED.code;
        let green=parameter.GREEN.code;
        let blue=parameter.BLUE.code;

        Generator.addCode(`(${red},${green},${blue})`)
   
    }

    //% block="RGB lights [NUMBER1] to [NUMBER2] display gradient color. tone from [RANGE1] to [RANGE2]" blockType="command"  
    //% NUMBER1.shadow="dropdown" NUMBER1.options="NUMBER"
    //% NUMBER2.shadow="dropdown" NUMBER2.options="NUMBER"
    //% RANGE1.shadow="range" RANGE1.params.min=0 RANGE1.params.max=360 RANGE1.defl=1
    //% RANGE2.shadow="range" RANGE2.params.min=0 RANGE2.params.max=360 RANGE2.defl=360
    export function myBlock13(parameter: any, block: any) { 
        let number1=parameter.NUMBER1.code;
        let number2=parameter.NUMBER2.code;
        let range1=parameter.RANGE1.code; 
        let range2=parameter.RANGE2.code; 

        Generator.addCode(`RGB.rainbow(${number1},${number2},${range1},${range2})`)
   
    }

    //% block="RGB light moves [MOVE] unit" blockType="command"  
    //% MOVE.shadow="number" MOVE.defl="0"
    export function myBlock14(parameter: any, block: any) { 
        let MOVE= parameter.MOVE.code

        Generator.addCode(`RGB.shift(${MOVE})`)
   
    }

    //% block="The RGB light rotates [MOVE] unit" blockType="command"  
    //% MOVE.shadow="number" MOVE.defl="0"
    export function myBlock15(parameter: any, block: any) { 
        let MOVE=parameter.MOVE.code;

        Generator.addCode(`RGB.rotate(${MOVE})`)
   
    }
}

    